from sqlalchemy.ext.asyncio import AsyncSession


class DatabaseMapper:
    def __init__(self, db_session: AsyncSession):
        self.db_session = db_session

    async def create_model(self, message, model_type, **kwargs):
        model = model_type(**kwargs)
        self.db_session.add(model)
        await self.db_session.flush()
        return model

    @staticmethod
    def _map_message_to_model(message, model_class, **kwargs):
        model_kwargs = {}
        for field in model_class.__table__.columns.keys():
            if hasattr(message, field):
                model_kwargs[field] = getattr(message, field)
            elif field in kwargs:
                model_kwargs[field] = kwargs[field]
        return model_kwargs
